import Vue from "vue";
import Vuex from "vuex";
import quinn from "@/utils/quinn";
import { conforms } from "lodash";

Vue.use(Vuex);

const state = {
  userData: {},
  cacheRouter: [],
  tokenv3: "",
  warehouseList: [],
  supplierList: [],
  aftersaleReasonList: [],
  areaList: [],
  roleList: [],
  roleMap: {},
  directoryMap: {},
  accountMap: {},
  leaderMap: {},
  aftersaleMap: {},
  authority: [],
  authorityList: [],
  categoryList:[]
};
const getters = {
  roleItems({ roleList: list }) {
    return list.map((e) => ({
      label: e.roleName,
      value: e.roleId,
    }));
  },
  aaa({ authorityList }) {
    // console.log(authorityList,"");
    return [1];
  },
  aftersaleReasonMap({ aftersaleReasonList }) {
    const res = {};
    aftersaleReasonList.forEach((e) => {
      res[e.value] = e.label;
    });
    return res;
  },
  areaItems({ areaList }) {
    return areaList.map((e) => ({
      label: e.name,
      value: `${e.area}`,
    }));
  },
  supplierListItems({ supplierList }) {
    return supplierList.map((e) => ({
      label: e.supplierName,
      value: e.supplierId,
    }));
  },
  categoryListItems({categoryList}){
    return categoryList.map((e) => ({
      label: e.name,
      value: e.classificationId,
    }));
  },
  warehouseListItems({ warehouseList }) {
    return warehouseList.map((e) => ({
      label: e.warehouseName,
      value: e.warehouseId,
    }));
  },
};

const mutations = {
  modify(state, map = {}) {
    for (const key in map) {
      if (Object.hasOwnProperty.call(map, key)) {
        const value = map[key];
        if (Object.hasOwnProperty.call(state, key)) state[key] = value;
      }
    }
  },
};

const actions = {
  refCategory(context){
    const { commit, state } = context;
    return  new Promise((resolve,reject)=>{
      try{
      window.$api.classificationClassificationList({'pageNumber':1})
      .then((res)=>{
        const { rows = [] } = res;
        commit("modify", {
          categoryList: rows,
        });
        resolve(res);
      }) .catch(reject);
      }catch(error){
       // console.log("error", error);
        reject(error);
      }
    })
  },
  refreshRole(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api.roleRoleList({ pageSize: 10000,pageNumber:1 })
          .then((res) => {
            const { rows = [] } = res;
            commit("modify", {
              roleList: rows,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAftersaleReason(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .afterSaleAfterSaleReasons({ pageSize: 10000,pageNumber:1 })
          .then((res) => {
            commit("modify", {
              aftersaleReasonList: res,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAuthority(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        const { id } = quinn.getStorage("userData") || {};
        window.$api
            .getAccountInfo({ id })
            .then((res) => {
              const { authorityList = [] } = quinn.getStorage("userDatav3");
              commit("modify", {
                authorityList,
              });
              resolve(res);
            })
            .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshArea(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .areaAreaList({ pageSize: 10000,pageNumber:1 })
          .then((res) => {
            const { rows = [] } = res;
            commit("modify", {
              areaList: rows,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshSupplier(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .supplierSupplierList({ pageSize: 10000,pageNumber:1 })
          .then((res) => {
            const { rows = [] } = res;
            commit("modify", {
              supplierList: rows,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshWarehouse(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .warehouseWarehousePullDownList()
          .then((res) => {
            // const { rows = [] } = res;
            commit("modify", {
              warehouseList: res, // wwarehouseList:rwos
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAftersale(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .newAfterSaleTypesEnum()
          .then((res) => {
            const map = {};
            (res || []).forEach(({ label, value }) => {
              map[value] = label;
            });
            commit("modify", {
              aftersaleMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshDirectory(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getDirectory({
            pageSize: 100000,
            pageNumber:1
          })
          .then((res) => {
            const map = {};
            (res.data || []).forEach(({ id, name }) => {
              map[id] = name;
            });
            commit("modify", {
              directoryMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAccount(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getAccount({
            pageNumber:1,
            pageSize: 100000,
          })
          .then((res) => {
            const map = {};
            const arr = res.data || [];
            arr.forEach(({ id, username, roleId, roleName }) => {
              map[id] = `${username} (${roleName})`;
            });
            commit("modify", {
              accountMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
  refreshLeader(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getLeaders({
            type: 3,
          })
          .then((res) => {
            const map = {};
            const arr = res || [];
            arr.forEach(({ id, username, roleId, roleName }) => {
              map[id] = `${username} (${roleName})`;
            });
            commit("modify", {
              leaderMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        // console.log("error", error);
        reject(error);
      }
    });
  },
};

const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
});

export default store;
